﻿using System;
using AdAggregator.Website;
using RealEstateAggregator.Models.Parsers;
using RealEstateAggregator.Models.SearchData;
using RealEstateAggregator.Utils;

namespace RealEstateAggregator.Models.Websites
{
    public class SzybkoPlWebsite : BaseWebsite<MySearchData,SzybkoPlSearchData, MyAdvertisement, SzybkoPlParser>
    {
        public SzybkoPlWebsite(SzybkoPlParser parser) : base(parser)
        {
        }

        protected override SzybkoPlSearchData MapSearchData(MySearchData searchData)
        {
            var data = new SzybkoPlSearchData
            {
                AdvertisementType = MapAdType(searchData.AdvertisementType),
                AreaMin = searchData.AreaMin.ToString(),
                AreaMax = searchData.AreaMax.ToString(),
                City = searchData.City,
                District = searchData.District,
                Street = searchData.Street,
                EstateType = MapEstateType(searchData.EstateType),
                MarketType = MapMarketType(searchData.MarketType),
                Keywords = searchData.Keywords,
                PriceMin = searchData.PriceMin.ToString(),
                PriceMax = searchData.PriceMax.ToString(),
                RoomCountMin = searchData.RoomCountMin.ToString(),
                RoomCountMax = searchData.RoomCountMax.ToString(),
                YearBuiltMin = searchData.YearBuiltMin.ToString(),
                YearBuiltMax = searchData.YearBuiltMax.ToString(),
                Voivodship = MapVoivodship(searchData.Voivodship),
                SpecialType = searchData.EstateType == EstateType.Garage ? "157" : "0",
                OnlyWithPhoto = searchData.OnlyWithPhoto,
                Page = searchData.Page
            };
            return data;
        }

        private string MapMarketType(MarketType type)
        {
            switch (type)
            {
                case MarketType.Primary:
                    return "0";
                case MarketType.Secondary:
                    return "1";
                default:
                    return "-";
            }
        }

        private string MapVoivodship(string v)
        {
            if (v != "") return "r" + Constants.Voivodships.IndexOf(v);
            return "0";
        }

        private string MapEstateType(EstateType type)
        {
            switch (type)
            {
                case EstateType.House:
                    return "house";
                case EstateType.Flat:
                    return "flat";
                case EstateType.Room:
                    return "room";
                case EstateType.Commercial:
                case EstateType.Garage:
                    return "komercyjne";
                case EstateType.Land:
                    return "land";
                default:
                    return String.Empty;
            }
        }

        private string MapAdType(AdvertisementType type)
        {
            if (type == AdvertisementType.Sale)
                return "sell";

            if (type == AdvertisementType.Rent)
                return "rent";

            return String.Empty;
        }
    }
}